<?php
/**
 * SettingsController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/** action cacher */
require_once 'Madoqua/Cache/Action.php';

/**
 * BlogAdmin_SettingsController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_SettingsController extends Madoqua_Controller_Action_Abstract
{
    /**
     * the file name of the xml configuration data
     *
     * @var string
     */
    private $_file;

    /**
     * simple xml representation of the config data
     *
     * @var SimpleXMLElement
     */
    private $_xml;

    /**
     * initialization
     */
    public function init()
    {
        parent::init();

        $this->_file = Madoqua_Application::getApplicationDirectory() . DIRECTORY_SEPARATOR . 'config/madoqua.xml';
        $this->_xml = simplexml_load_file($this->_file);
        //simple xml of the config

    }

    /**
     * index of settings (displaying all we can configure
     */
    public function indexAction()
    {

    }

    /**
     * settings for spam-finding for comments
     */
    public function commentsAction()
    {
        $form = $this->_formLoader->get();
        $form->setDefaults((array) $this->_xml->comments);
        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            //form is valid
            $this->save($form, 'comments');
        }

        $this->view->form = $form;
    }

    /**
     * caching related settings
     */
    public function cachingAction()
    {
        $enableForm = $this->_formLoader->get();
        //enabling caching
        $enableForm->setDefaults((array) $this->_xml->caching);
        if ($this->getRequest()->isPost() && $enableForm->isValid($this->getRequest()->getPost())) {
            //form is valid
            $this->save($enableForm, 'caching');
        }

        $this->view->enableForm = $enableForm;

        $resetForm = $this->_formLoader->get('BlogAdmin_Settings_CachingReset');
        //reset form
        $resetForm->setRouteAction(array('action' => 'caching-reset'));
        //cachingReset as action
        $this->view->resetForm = $resetForm;
        //goes to the view too :)
    }

    /**
     * reset all caches
     */
    public function cachingResetAction()
    {
        $this->getHelper('viewRenderer')->setNeverRender();
        //no view renderering
        $cache = Madoqua_Cache_Action::get();
        //get the action cacher
        $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
        //clear all caches
        $this->getHelper('redirector')->gotoRoute(
        array(
                    'controller' => 'settings', 
                    'action' => 'caching'
                    ),
            'blog_admin'
            );
    }

    /**
     * some people just want to hack everything :(
     */
    public function xmlAction()
    {
        $form = $this->_formLoader->get();
        //the form
        $form->setDefault('xml', file_get_contents($this->_file));
        //set the xml to be the default value for field xml

        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            //form is valid
            if (!file_put_contents($this->_file, $form->getValue('xml'))) {
                throw new Exception('Could not save config file');
            }

            $this->getHelper('redirector')->gotoRoute(
            array(
                    'controller' => 'settings', 
                    'action' => 'xml'
                    ),
                'blog_admin'
                );
        }

        $this->view->form = $form;
    }

    /**
     * save the xml back to a file
     *
     * @param Zend_Form $form
     * @param string $section
     * @return void
     */
    private function save($form, $section)
    {
        foreach($this->_xml->$section->children() as $name => $child) {
            $this->_xml->$section->$name = $form->getValue($name);
        }

        $this->_xml->asXML($this->_file);
    }

}